package funcs

import (
	"strconv"
	"strings"

	"github.com/toolkits/pkg/file"
	"github.com/toolkits/pkg/logger"

	"github.com/didi/nightingale/src/common/dataobj"
	"github.com/didi/nightingale/src/modules/agent/core"
)

func UptimeMetrics() (ret []*dataobj.MetricValue) {
	content, err := file.ToTrimString("/proc/uptime")
	if err != nil {
		logger.Warning(err)
		return
	}

	fields := strings.Fields(content)
	if len(fields) < 2 {
		return
	}

	uptime, err := strconv.ParseFloat(fields[0], 64)
	if err != nil {
		logger.Warning(err)
		return
	}

	ret = []*dataobj.MetricValue{core.GaugeValue("sys.uptime", uptime)}
	return
}
